package com.zbkj.admin.controller.platform;

import cn.hutool.core.map.MapUtil;
import cn.idev.excel.write.metadata.style.WriteFont;
import com.amazonaws.util.IOUtils;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.zbkj.admin.request.OrderSearchRequest;
import com.zbkj.admin.result.CommonResult;
import com.zbkj.admin.service.EbDividendArraignService;
import com.zbkj.admin.service.ExportService;
import com.zbkj.admin.vo.ArraignExcelVo;
import com.zbkj.admin.vo.OrderExcelVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 *  平台端导出控制器
 *  +----------------------------------------------------------------------
 *  | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 *  +----------------------------------------------------------------------
 *  | Author: CRMEB Team <admin@crmeb.com>
 *  +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/platform/export")
@Tag(name = "平台端导出控制器")
public class PlatformExportController {

    @Autowired
    private ExportService exportService;
    @Autowired
    private EbDividendArraignService ebDividendArraignService;

    @ResponseExcel(name = "订单信息")
    @PreAuthorize("hasAuthority('platform:export:order:excel')")
    @Operation(summary = "导出订单Excel")
    @RequestMapping(value = "/order/excel", method = RequestMethod.GET)
    public List<OrderExcelVo> exportOrder(@Validated OrderSearchRequest request){
//        String fileName = exportService.exportOrder(request);
//        HashMap<String, String> map = MapUtil.newHashMap();
//        map.put("fileName", fileName);
        return exportService.exportOrder(request);
    }



    @Operation(summary = "导出提现信息Excel")
    @RequestMapping(value = "/arraign/excel", method = RequestMethod.GET)
    public void exportArraign(HttpServletResponse response, @RequestParam(value = "examineAndApproveState", required = false) String examineAndApproveState,
                              @RequestParam(value = "startDate", required = false) LocalDateTime startDate,
                              @RequestParam(value = "endDate", required = false) LocalDateTime endDate){
        try {
            // 查询提现数据
            List<ArraignExcelVo> data = ebDividendArraignService.exportArraign(examineAndApproveState, startDate, endDate);
            // 生成excel表格
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("提现信息");

            // 设置列宽
            sheet.setColumnWidth(0, 7000);
            sheet.setColumnWidth(1, 12000);
            sheet.setColumnWidth(2, 7000);
            sheet.setColumnWidth(3, 9000);

            // 设置表头字体
            HSSFFont font = wb.createFont();
            font.setBold(true); // 设置加租
            font.setFontHeightInPoints((short) 16); // 设置字体大小
            HSSFCellStyle headerStyle = wb.createCellStyle();
            headerStyle.setFont(font);
            headerStyle.setWrapText(true); // 设置自动换行
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            // 设置红色
            HSSFFont redFont = wb.createFont();
            redFont.setColor(HSSFColor.RED.index);// 红色
            redFont.setFontHeightInPoints((short) 13); // 设置字体大小

            // 定义变量对象
            HSSFRow row = null;
            HSSFCell cell = null;
            // 设置表头
            row = sheet.createRow(0);
            row.setHeightInPoints(40);
            cell = row.createCell(0);
            //创建富文本字符串，内容为：Hello，World！ 设置redFont字体
            HSSFRichTextString richString0 = new HSSFRichTextString("收款方名称\n(必输，文本格式)");
            richString0.applyFont( 5, 15, redFont);
            //将富文本字符串设置到单元格中
            cell.setCellValue(richString0);
            cell.setCellStyle(headerStyle);

            cell = row.createCell(1);
            //创建富文本字符串，内容为：Hello，World！ 设置redFont字体
            HSSFRichTextString richString1 = new HSSFRichTextString("收款方账号\n(必输，本文格式，支持银行和支付宝)");
            richString1.applyFont( 5, 24, redFont);
            cell.setCellValue(richString1);
            cell.setCellStyle(headerStyle);

            cell = row.createCell(2);
            //创建富文本字符串，内容为：Hello，World！ 设置redFont字体
            HSSFRichTextString richString2 = new HSSFRichTextString("金额\n(必输，文本格式)");
            richString2.applyFont( 2, 12, redFont);
            cell.setCellValue(richString2);
            cell.setCellStyle(headerStyle);

            cell = row.createCell(3);
            //创建富文本字符串，内容为：Hello，World！ 设置redFont字体
            HSSFRichTextString richString3 = new HSSFRichTextString("附言/用途\n(必输，文本格式，最多40字)");
            richString3.applyFont( 5, 21, redFont);
            cell.setCellValue(richString3);
            cell.setCellStyle(headerStyle);


            // 设置锁定样式
            HSSFFont lockFont = wb.createFont();
            lockFont.setFontHeightInPoints((short) 12);
            HSSFCellStyle lockstyle = wb.createCellStyle();
            lockstyle.setLocked(true);//设置锁定
            lockstyle.setFont(lockFont);
            // 设定未锁定样式
            HSSFCellStyle unlockStyle=wb.createCellStyle();
            unlockStyle.setLocked(false);//设置未锁定
            unlockStyle.setFont(lockFont);

            for(int i = 0; i < data.size(); i++){
                // 创建行数据
                row = sheet.createRow(i + 1);
                // 设置行高
                row.setHeightInPoints(18);
                // 保存数据
                cell = row.createCell(0);
                cell.setCellStyle(lockstyle);//将需要上锁的单元格进行锁定
                cell.setCellValue(data.get(i).getUserName());

                cell = row.createCell(1);
                cell.setCellStyle(lockstyle);//将需要上锁的单元格进行锁定
                cell.setCellValue(data.get(i).getBankCode());

                cell = row.createCell(2);
                cell.setCellStyle(lockstyle);//将需要上锁的单元格进行锁定
                cell.setCellValue(data.get(i).getAmountOfMoney());

                cell = row.createCell(3);
                cell.setCellStyle(unlockStyle);//默认是锁定状态；将所有单元格设置为：未锁定；然后再对需要上锁的单元格单独锁定
                cell.setCellValue("销售津贴");
            }
            //sheet添加保护，这个一定要否则光锁定还是可以编辑的
            sheet.protectSheet("123456");
            // 生成流对象
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            wb.write(bos);
            // 生成Excel文件并返回流
            InputStream is = new ByteArrayInputStream(bos.toByteArray());
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=提现信息.xls");
            // 将流返回给前端
            IOUtils.copy(is, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



